package org.dragonnova.meetingapp.app.fragment.guide;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.app.activity.WebActivity;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link RouterFragment.OnFragmentInteractionListener} interface
 * to handle interact events.
 * Use the {@link RouterFragment#newInstance} factory method to
 * created an instance of this fragment.
 */
public class RouterFragment extends FragmentImpl implements View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View mContentView;

    public RouterFragment() {
        // Required empty public constructor
    }

    public static RouterFragment newInstance(String param1, String param2) {
        RouterFragment fragment = new RouterFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_router, container, false);
        initView();
        return mContentView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
    }

    public void initView() {
        ImageView mguide_iv_web = (ImageView) mContentView.findViewById(R.id.mguide_iv_web);
        TextView mguide_tv_router_skip3 = (TextView) mContentView.findViewById(R.id.mguide_tv_router_skip3);

        mguide_iv_web.setOnClickListener(this);
        mguide_tv_router_skip3.setOnClickListener(this);
    }

    private void initData() {

    }

    // TODO: Rename method, updateMeeting argument and hook method into UI event
    public void interact(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.mguide_iv_web:
                Intent webIntent = new Intent(getActivity(), WebActivity.class);
                webIntent.putExtra("ip", mParam1);
                startActivityForResult(webIntent, 0);
                break;
            case R.id.mguide_tv_router_skip3:
                Uri uri = Uri.parse("content://RouterFragment" + "/" + "skip");
                interact(uri);
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
